package com.doupi.partner.rpc.biz;

import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PageData;
import com.doupi.partner.model.UserRelation;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.service.UserRelationService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户关系
 *
 * @author kxj
 * @version 2017-04-17 21:57
 **/
@Component("userRelationBiz")
public class UserRelationBizImpl implements UserRelationBiz {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRelationBizImpl.class);

    private final UserRelationService userRelationService;

    private final BusinessRelationService businessRelationService;

    @Autowired
    public UserRelationBizImpl(UserRelationService userRelationService, BusinessRelationService businessRelationService) {
        this.userRelationService = userRelationService;
        this.businessRelationService = businessRelationService;
    }

    @Override
    public synchronized int addUserRelation(UserRelation userRelation) {
        // TODO 做些校验
        LOGGER.info("[partner-rpc] - 收到用户关系{}", userRelation);

        // 查询是否处理过
        UserRelation userRelationParam = new UserRelation();
        userRelationParam.setBusinessId(userRelation.getBusinessId());
        userRelationParam.setUserId(userRelation.getUserId());
        int count = userRelationService.countUserRelation(userRelationParam);
        if (count > 0) {
            return 0;
        }

        LOGGER.info("[partner-rpc] - 未处理过用户关系");

        BusinessRelation businessRelation = new BusinessRelation();
        businessRelation.setBusinessId(userRelation.getBusinessId());
        List<BusinessRelation> businessRelationList = businessRelationService.queryBusinessRelations(businessRelation, new RowBounds(0, 1));
        LOGGER.info("[partner-rpc] - 查询到商户{}", businessRelationList);
        if (!businessRelationList.isEmpty()) {
            businessRelation = businessRelationList.get(0);
            userRelation.setBusinessAdministratorId(businessRelation.getAdministratorId());
            userRelation.setPartnerAdministratorId(businessRelation.getPartnerAdministratorId());
            userRelation.setSuperiorPartnerAdministratorId(businessRelation.getSuperiorPartnerAdministratorId());
            userRelation.setCityAdministratorId(businessRelation.getCityAdministratorId());
            userRelation.setSubstationAdministratorId(businessRelation.getSubstationAdministratorId());
            userRelation.setCity(businessRelation.getCity());
            // TODO 粉丝类型
            // userRelation.setType((byte) 1);

            return userRelationService.insert(userRelation);
        }
        return -1;
    }

    @Override
    public PageData<UserRelation> queryUserRelationByPageData(Integer offset, Integer limit, UserRelation userRelation) {
        Pagination<UserRelation, UserRelation> pagination = new Pagination<>(offset, limit, userRelation);
        pagination = userRelationService.queryUserRelations(pagination);
        return new PageData<>(pagination.getTotalCount(), pagination.getRecords());
    }

}
